package com.hengtiansoft.lfy.chart.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;

import java.util.ArrayList;
import java.util.HashMap;

public class MiniPopupWindowAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mListItem;
    private ArrayList<HashMap<String, Object>> mItemTitleString;
    private int position;

    public MiniPopupWindowAdapter(Context context,
                                  ArrayList<HashMap<String, Object>> strTitle, int position) {
        super();
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        this.mItemTitleString = strTitle;
        this.position = position;
        getData();
    }

    private ArrayList<HashMap<String, Object>> getData() {
        int size = mItemTitleString.size();
        mListItem = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map;
        for (int i = 0; i < position; i++) {
            map = new HashMap<String, Object>();
            map.put("itemTitle", mItemTitleString.get(i).get("one"));
            map.put("isSelect", false);
            map.put("itemTitleTwo", mItemTitleString.get(i).get("two"));
            mListItem.add(map);
        }
        map = new HashMap<String, Object>();
        map.put("itemTitle", mItemTitleString.get(position).get("one"));
        map.put("isSelect", true);
        map.put("itemTitleTwo", mItemTitleString.get(position).get("two"));
        mListItem.add(map);
        for (int i = position + 1; i < size; i++) {
            map = new HashMap<String, Object>();
            map.put("itemTitle", mItemTitleString.get(i).get("one"));
            map.put("isSelect", false);
            map.put("itemTitleTwo", mItemTitleString.get(i).get("two"));
            mListItem.add(map);
        }
        return mListItem;
    }

    @Override
    public int getCount() {
        if (mListItem != null) {
            return mListItem.size();
        } else {
            return 0;
        }
    }

    @Override
    public HashMap<String, Object> getItem(int id) {
        return mListItem.get(id);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public ArrayList<HashMap<String, Object>> getListItem() {
        return mListItem;
    }

    public void setListItem(ArrayList<HashMap<String, Object>> listItem) {
        this.mListItem = listItem;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.listitem_popup_window_first_mini, null);
                holder.tvItemCreditorSelect = (TextView) convertView
                        .findViewById(R.id.tv_item_select);
                holder.tvItemCreditorSelectSecond = (TextView) convertView
                        .findViewById(R.id.tv_item_select_second);
                holder.llListviewItem = (LinearLayout) convertView
                        .findViewById(R.id.ll_listview_item);
                holder.ivChangeLanguage = (ImageView) convertView
                        .findViewById(R.id.iv_change);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvItemCreditorSelect.setText(mListItem.get(position)
                    .get("itemTitle").toString());
            holder.tvItemCreditorSelectSecond.setText(mListItem.get(position)
                    .get("itemTitleTwo").toString());
            if ((Boolean) mListItem.get(position).get("isSelect")) {
                holder.tvItemCreditorSelectSecond.setTextColor(mContext
                        .getResources().getColor(R.color.background_blue));
                holder.tvItemCreditorSelect.setTextColor(mContext
                        .getResources().getColor(R.color.background_blue));
                holder.ivChangeLanguage
                        .setImageResource(R.drawable.interpret_change_language_blue);
            } else {
                holder.ivChangeLanguage
                        .setImageResource(R.drawable.interpret_listview_change);
                holder.tvItemCreditorSelect.setTextColor(mContext
                        .getResources().getColorStateList(R.color.gray));
                holder.tvItemCreditorSelectSecond.setTextColor(mContext
                        .getResources().getColorStateList(R.color.gray));
            }

        } else {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.listitem_popup_window_mini, null);
                holder.tvItemCreditorSelect = (TextView) convertView
                        .findViewById(R.id.tv_item_select);
                holder.tvItemCreditorSelectSecond = (TextView) convertView
                        .findViewById(R.id.tv_item_select_second);
                holder.llListviewItem = (LinearLayout) convertView
                        .findViewById(R.id.ll_listview_item);
                holder.ivChangeLanguage = (ImageView) convertView
                        .findViewById(R.id.iv_change);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvItemCreditorSelect.setText(mListItem.get(position)
                    .get("itemTitle").toString());
            holder.tvItemCreditorSelectSecond.setText(mListItem.get(position)
                    .get("itemTitleTwo").toString());
            if ((Boolean) mListItem.get(position).get("isSelect")) {
                holder.tvItemCreditorSelectSecond.setTextColor(mContext
                        .getResources().getColor(R.color.background_blue));
                holder.tvItemCreditorSelect.setTextColor(mContext
                        .getResources().getColor(R.color.background_blue));
                holder.ivChangeLanguage
                        .setImageResource(R.drawable.interpret_change_language_blue);
            } else {
                holder.ivChangeLanguage
                        .setImageResource(R.drawable.interpret_listview_change);
                holder.tvItemCreditorSelect.setTextColor(mContext
                        .getResources().getColorStateList(R.color.gray));
                holder.tvItemCreditorSelectSecond.setTextColor(mContext
                        .getResources().getColorStateList(R.color.gray));
            }
        }

        return convertView;
    }

    public final class ViewHolder {
        TextView tvItemCreditorSelect;
        TextView tvItemCreditorSelectSecond;
        ImageView ivChangeLanguage;
        LinearLayout llListviewItem;
    }
}
